package jsyf.oa.service.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.dao.wreport.WReportExpensesDao;
import jsyf.oa.entity.wreport.WReportExpensesEntity;
import jsyf.oa.facade.wreport.WReportExpensesFacade;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/3/22.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WReportExpensesService implements WReportExpensesFacade {
    @Autowired
    private WReportExpensesDao wReportExpensesDao;
    @Autowired
    private UserDao userDao;
    @Override
    public List<JSONObject> getPhoneFees(JSONObject jsonObject){
        String userId=jsonObject.getString("userId");
        JSONObject userJson = userDao.getViewJSONListByCondition(" AND id = ? AND main = 1 ",new Object[]{userId}).get(0);
        String roleNameId=userJson.getString("roleNameId");
        List<JSONObject> records=wReportExpensesDao.getByCondition("AND TEMPORARY_COST= ?",new Object[]{"0"});
        List<JSONObject> getOilList =wReportExpensesDao.getOilList("",new Object[]{});
        for (JSONObject o:getOilList){
            if(o.getString("roleNameId").equals(roleNameId)){
                List<JSONObject> newOilList =wReportExpensesDao.getOilList(" AND ROLE_NAME_ID=?",new Object[]{roleNameId});
                return  newOilList;
            }
        }
        for (int i = records.size() - 1; i >= 0; i--) {
            if ("现金加油费".equals(records.get(i).getString("phoneName")) || "油卡加油费".equals(records.get(i).getString("phoneName"))) {
                records.remove(i);
            }
        }
        return records;
    }

    @Override
    public String addPhoneFees(String jsonData){
        List<WReportExpensesEntity> entity = (List<WReportExpensesEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), WReportExpensesEntity.class);
        for (int i = 0; i < entity.size(); i++) {
            WReportExpensesEntity model = entity.get(i);
            if(model.getParentId() != 0){
                wReportExpensesDao.add(model);
            }
        }
        return "true";

    }

//    @Override
//    public JSONObject delPhoneFees(String jsonData){
//        JSONObject jsonObject = new JSONObject();
//        if (wReportExpensesDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0){
//            jsonObject.put("records","删除成功");
//        }else{
//            jsonObject.put("records","删除失败");
//        };
//        return jsonObject;
//
//    }

    @Override
    public Boolean remove(String jsonData) {
       return wReportExpensesDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public JSONObject updatePhoneFees(String jsonData) {
        JSONObject jsonObject= new JSONObject();
        List<WReportExpensesEntity> entity = (List<WReportExpensesEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), WReportExpensesEntity.class);
        for (int i = 0; i < entity.size(); i++) {
            WReportExpensesEntity model = entity.get(i);
            WReportExpensesEntity oldEntity = (WReportExpensesEntity) wReportExpensesDao.getIBaseEntityById(model.getId());
            if(model.getId() != null){
                oldEntity.setId(model.getId());
            }
            if(model.getParentId() != null){
                oldEntity.setParentId(model.getParentId());
            }
            if(model.getName() != null){
                oldEntity.setName(model.getName());
            }
            if(model.getPhoneName() != null){
                oldEntity.setPhoneName(model.getPhoneName());
            }
            if(model.getTemporaryCost() != null){
                oldEntity.setTemporaryCost(model.getTemporaryCost());
            }
            wReportExpensesDao.update(oldEntity);
        }
        jsonObject.put("records","修改成功");
        return jsonObject;
    }



}